﻿
function moneyrange(value, element, param)
{
    return Number(value) % 100 == 0 && Number(value)>0;
}

function datetimerangebystarttimeandendtime(value,element,param)
{
    if (value.length <= 0)
        return true;
    var dateTime = new Date(value);
    var sTime = $('#' + param.s1).val();
    var eTime = $('#' + param.e1).val();
    return dateTime > new Date(sTime) && dateTime < new Date(eTime);
}

function datetimegreaterdatetimenow(value, element, param)
{
    var dateTime = new Date(value);
    var nowDate = new Date();
    nowDate.setDate(nowDate.getDate());
    return dateTime >=new Date( nowDate.getFullYear(),nowDate.getMonth(),nowDate.getDate());
}


function datetimegreaterdatetime(value, element, ot) {
    var dateTime = new Date(value);
    var oTime = new Date($('#' + ot).val());
    return dateTime >= oTime.setDate(oTime.getDate() + 1);
}

function TraceBorrowMoneyAndBorrowDeadline() {
    var monthlyInterest =Number($('#InterestRate').val());
    var borrowMoneyValue = $('#BorrowMoney').val();
    if (moneyrange(borrowMoneyValue) && BorrowDeadlineValidate()
        && monthlyInterest>0) {
        var BorrowDeadlineValue = Number($('#BorrowDeadline').val());
        borrowMoneyValue = Number(borrowMoneyValue);
        var totalRepaymentValue = TotalRepaymentAmountCompute((monthlyInterest / 100), BorrowDeadlineValue, borrowMoneyValue);
        var monthRepaymentAmount = TheMonthlyRepaymentAmountCompute((monthlyInterest / 100), BorrowDeadlineValue, borrowMoneyValue);

        $('#totalRepayment').html(totalRepaymentValue);
        $('#monthRepayment').html(monthRepaymentAmount);
    }
    else
    {
        $('#totalRepayment').html("正在计算...");
        $('#monthRepayment').html("正在计算...");
    }
}

jQuery.validator.setDefaults({
    ignore: []
});

jQuery.validator.addMethod("moneyrange", moneyrange, "借款金额必须是100的倍数.");
  
jQuery.validator.unobtrusive.adapters.add("moneyrange", [], function (options) {
    options.rules["moneyrange"] = {
 };
    options.messages["moneyrange"] = options.message;
});

jQuery.validator.addMethod("datetimerangebystarttimeandendtime", datetimerangebystarttimeandendtime, "投标结束时间必须小于借款结束时间并且大于借款开始时间.");

jQuery.validator.unobtrusive.adapters.add("datetimerangebystarttimeandendtime", ['s1','e1'], function (options) {
    options.rules["datetimerangebystarttimeandendtime"] = {s1:options.params.s1,e1:options.params.e1};
    options.messages["datetimerangebystarttimeandendtime"] = options.message;
});


jQuery.validator.addMethod("datetimegreaterdatetimenow", datetimegreaterdatetimenow);
jQuery.validator.addMethod("datetimegreaterdatetime", datetimegreaterdatetime);
jQuery.validator.unobtrusive.adapters.addBool('datetimegreaterdatetimenow');

jQuery.validator.unobtrusive.adapters.addSingleVal('datetimegreaterdatetime', 'ot');

function BorrowDeadlineValidate()
{
    return Number($('#BorrowDeadline').val()) > 0;
}

$(document).ready(function () {

    //$('#BorrowMoney').change(TraceBorrowMoneyAndBorrowDeadline);
    //$('#InterestRate').change(TraceBorrowMoneyAndBorrowDeadline);

    var datepickerOptions=
        {
            dateFormat: "yy-mm-dd",
            dayNamesMin: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
            monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            minDate:new Date().setDate(new Date().getDate()+1),
        }


    $('#StartTime').datepicker(datepickerOptions);
$('#EndTime').datepicker(datepickerOptions);
$('#BorrowDeadline').datepicker(datepickerOptions);

    $('#saveBtn').click(function () {
        $('form').attr('action', "/BorrowStep1/Save").submit();
    });

    $('#nextBtn').click(function () {
        $('form').submit();
    });
});